package com.lagou.service.impl;

import com.github.dozermapper.core.Mapper;
import com.lagou.config.MailProperties;
import com.lagou.pojo.MailDTO;
import com.lagou.service.MailService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.StringUtils;

/**
 * @ClassName MailServiceImpl
 * @Description
 * @Author playboy
 * @Date 2021/6/25 4:29 下午
 * @Version 1.0
 **/
@Service
public class MailServiceImpl implements MailService {
	private static Logger log = LoggerFactory.getLogger(MailServiceImpl.class);

	@Autowired
	private MailProperties mailProperties;

	@Autowired
	private JavaMailSender javaMailSender;

	@Autowired
	private Mapper mapper;

	@Override
	public boolean email(String email, String code) {
		boolean flag = true;
		MailDTO mailDTO = new MailDTO();
		mailDTO.setText("验证码：" + code);
		mailDTO.setSubject("验证码");
		mailDTO.setTo(new String[]{email});
		if (StringUtils.isEmpty(mailDTO.getFrom())) {
			mailDTO.setFrom(mailProperties.getFrom());
			//抄送给自己
			mailDTO.setCc(new String[]{mailProperties.getFrom()});
		}
		SimpleMailMessage simpleMailMessage = mapper.map(mailDTO, SimpleMailMessage.class);
		try {
			javaMailSender.send(simpleMailMessage);
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
}
